package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalPersonTypeDao;
import com.boss.salary.entity.SalPersonType;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class SalPersonTypeDaoImpl extends BaseDaoImpl implements ISalPersonTypeDao {

    @Override
    public List<SalPersonType> querySalPersonTypePage(PaginationDTO paginationDTO) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,code,name,create_time,update_time ")
                .append(" from ").append(TableName.SAL_PERSON_TYPE)
                .append(" where 1=1 ");
        sql.append(" order by create_time ");
        String querySql = this.boxUpSql(sql.toString());
        Object[] params = boxUpParam(paramList.toArray(), paginationDTO);
        return this.getjdbcTemplate().query(querySql, new BeanPropertyRowMapper<>(SalPersonType.class), params);
    }

    @Override
    public int querySalPersonTypeCount() {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select count(*) ")
                .append(" from ").append(TableName.SAL_PERSON_TYPE)
                .append(" where 1=1 ");
        sql.append(" order by create_time ");
        return this.getjdbcTemplate().queryForObject(sql.toString(), paramList.toArray(), Integer.class);
    }

    @Override
    public int queryDataByCode(String code) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT COUNT(*) FROM ").append(TableName.SAL_PERSON_TYPE);
        sql.append(" WHERE code = ? ");
        return this.getjdbcTemplate().queryForObject(sql.toString(), new Object[]{code}, Integer.class);
    }

    @Override
    public void insertSalPersonType(SalPersonType salPersonType) {
        StringBuilder sql = new StringBuilder();
        sql.append(" insert into ").append(TableName.SAL_PERSON_TYPE);
        sql.append(" (id, create_time, update_time, code, name) ")
                .append(" values (?,?,?,?,?) ");
        Object[] params = new Object[]{
                salPersonType.getId(), salPersonType.getCreateTime(), salPersonType.getUpdateTime(),
                salPersonType.getCode(), salPersonType.getName()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updateSalPersonType(SalPersonType salPersonType) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.SAL_PERSON_TYPE);
        sql.append(" set create_time=?, update_time=?, code=?, name=? ").append(" where id = ? ");
        Object[] params = new Object[]{
                salPersonType.getCreateTime(), salPersonType.getUpdateTime(),
                salPersonType.getCode(), salPersonType.getName(), salPersonType.getId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void deleteSalPersonType(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append(" delete from ").append(TableName.SAL_PERSON_TYPE)
                .append(" where id=? ");
        Object[] params = new Object[]{id};
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public List<SalPersonType> querySalPersonTypeAll(Map<String, Object> queryParam) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,code,name,create_time,update_time ")
                .append(" from ").append(TableName.SAL_PERSON_TYPE)
                .append(" where 1=1 ");
        if (!StringUtils.isEmpty(queryParam.get("id"))) {
            sql.append(" and id = ? ");
            paramList.add(queryParam.get("id"));
        }
        sql.append(" order by create_time ");
        return this.getjdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<>(SalPersonType.class), paramList.toArray());
    }

}
